

package com.wake.widget.widget.dialog;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wake.widget.R;
import com.wake.widget.util.NMUIResHelper;
import com.wake.widget.widget.NMUIWrapContentScrollView;
import com.wake.widget.widget.textview.NMUISpanTouchFixTextView;

import androidx.annotation.Nullable;

/**
 */
public class NMUIDialogBlockBuilder extends NMUIDialogBuilder<NMUIDialogBlockBuilder> {
    private CharSequence mContent;


    public NMUIDialogBlockBuilder(Context context) {
        super(context);
        setActionDivider(1, R.attr.nmui_skin_support_dialog_action_divider_color, 0, 0);
    }


    public NMUIDialogBlockBuilder setContent(CharSequence content) {
        mContent = content;
        return this;
    }

    public NMUIDialogBlockBuilder setContent(int contentRes) {
        mContent = getBaseContext().getResources().getString(contentRes);
        return this;
    }

    @Nullable
    @Override
    protected View onCreateTitle(NMUIDialog dialog, NMUIDialogView parent, Context context) {
        View result = super.onCreateTitle(dialog, parent, context);
        if(result != null && (mContent == null || mContent.length() == 0)){
            TypedArray a = context.obtainStyledAttributes(null,
                    R.styleable.NMUIDialogTitleTvCustomDef, R.attr.nmui_dialog_title_style, 0);
            int count = a.getIndexCount();
            for (int i = 0; i < count; i++) {
                int attr = a.getIndex(i);
                if (attr == R.styleable.NMUIDialogTitleTvCustomDef_nmui_paddingBottomWhenNotContent) {
                    result.setPadding(
                            result.getPaddingLeft(),
                            result.getPaddingTop(),
                            result.getPaddingRight(),
                            a.getDimensionPixelSize(attr, result.getPaddingBottom())
                    );
                }
            }
            a.recycle();
        }
        return result;
    }

    @Override
    @Nullable
    protected View onCreateContent(NMUIDialog dialog, NMUIDialogView parent, Context context) {
        if(mContent != null && mContent.length() > 0){
            TextView contentTv = new NMUISpanTouchFixTextView(context);
            NMUIResHelper.assignTextViewWithAttr(contentTv, R.attr.nmui_dialog_message_content_style);

            if (!hasTitle()) {
                TypedArray a = context.obtainStyledAttributes(null,
                        R.styleable.NMUIDialogMessageTvCustomDef,
                        R.attr.nmui_dialog_message_content_style, 0);
                int count = a.getIndexCount();
                for (int i = 0; i < count; i++) {
                    int attr = a.getIndex(i);
                    if (attr == R.styleable.NMUIDialogMessageTvCustomDef_nmui_paddingTopWhenNotTitle) {
                        contentTv.setPadding(
                                contentTv.getPaddingLeft(),
                                a.getDimensionPixelSize(attr, contentTv.getPaddingTop()),
                                contentTv.getPaddingRight(),
                                contentTv.getPaddingBottom()
                        );
                    }
                }
                a.recycle();
            }
            contentTv.setText(mContent);
            return wrapWithScroll(contentTv);
        }
        return null;
    }

    @Override
    public NMUIDialog create(int style) {
        setActionContainerOrientation(VERTICAL);
        return super.create(style);
    }
}
